package com.itzdm.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itzdm.reggie.common.basecontext.BaseContext;
import com.itzdm.reggie.entity.AddressBook;
import com.itzdm.reggie.mapper.AddressBookMapper;
import com.itzdm.reggie.service.AddressBookService;
import org.springframework.stereotype.Service;

@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {

    @Override
    public void setDefault(AddressBook addressBook) {
        LambdaUpdateWrapper<AddressBook> queryWrapper = new LambdaUpdateWrapper<>();
        //将当前所有的地址设为不是默认地址
        queryWrapper.eq(AddressBook::getId, BaseContext.getCurrentId());
        queryWrapper.set(AddressBook::getIsDefault, 0);
        update(queryWrapper);

        //将当前传过来的设为默认地址
        addressBook.setIsDefault(1);
        updateById(addressBook);
    }
}
